import CryptoJS from 'crypto-js';
import { MD5 } from 'crypto-js';
import urlencode from 'urlencode';
// import md5 from 'js-md5';

// 腾讯云签名
function QMFun(data) {
  const timestamp = Math.trunc(new Date().getTime() / 1000);

  const expired = Math.trunc(new Date().getTime() / 1000 + 3600);

  const nonce = randomString(10, '0123456789');

  const engine_model_type = '16k_zh';
  const voice_id = randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz');

  let SecretKey = `asr.cloud.tencent.com/asr/v2/${data.Appid}?engine_model_type=${engine_model_type}&expired=${expired}&filter_dirty=1&filter_modal=1&filter_punc=1&needvad=1&nonce=${nonce}&secretid=${data.SecretId}&timestamp=${timestamp}&voice_format=1&voice_id=${voice_id}`;
  const signature = Hmacstr(SecretKey, 'vfDbvIXbeXFlBcv53pbAYu56wa3MUgkd');

  return {
    signature: signature,
    SecretKey: SecretKey,
  };
}

//随机生成字符串
function randomString(length, chars) {
  var result = '';
  for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
  return result;
}

//HmacSHA1加密
function Hmacstr(e, key) {
  let result = CryptoJS.HmacSHA1(e, key);
  let sig = result.toString(CryptoJS.enc.Base64);
  sig = encodeURI(sig);
  sig = urlencode(sig);
  return sig;
}

// function urlencodeFun(str) {
//   str = (str + '').toString();
//   return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
// }

// //随机生成字符串
// function randomString(length, chars) {
//   var result = '';
//   for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
//   return result;
// }

//  参数加密
function QMdata(data) {
  // 随机字符串
  let Nonce = randomString('32', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  let Time = Math.trunc(new Date().getTime() / 1000);
  let Appid = 'VoiceRecord';
  let AppSecret = 'sz4QIuQLNkZXB0AFIMBmmRIjSRiJSa03';
  let Sign = MD5(`Appid=${Appid}&AppSecret=${AppSecret}&Nonce=${Nonce}&Time=${Time}&data=${JSON.stringify(data)}`)
    .toString()
    .toUpperCase();
  // console.log(Sign);
  return {
    Appid: 'VoiceRecord',
    AppSecret: 'sz4QIuQLNkZXB0AFIMBmmRIjSRiJSa03',
    Nonce: Nonce, // 32位随机字符串
    Time: Time, //当前时间戳（秒）
    Sign: Sign, //签名Appid = & AppSecret = & Nonce = & Time = & data = 拼凑成的字符串MD5加密转大写
    data: data,
  };
}

function KDFun(data) {
  let timestamp = Math.trunc(new Date().getTime() / 1000);
  // const expired = Math.trunc(new Date().getTime() / 1000 + 3600);
  // const nonce = randomString(10, '0123456789');
  // const voice_id = randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz');
  // timestamp = 1512041814;
  const baseString = data.Appid + timestamp;
  const md5baseString = MD5(`${baseString}`).toString();

  let signature = Hmacstr2(md5baseString, data.SecretId);
  console.log(signature);
  // let SecretKey = `asr.cloud.tencent.com/asr/v2/${data.Appid}`;
  // const signature = Hmacstr(SecretKey, 'vfDbvIXbeXFlBcv53pbAYu56wa3MUgkd');

  return {
    signature: signature,
    timestamp: timestamp,
  };
}

//HmacSHA1加密  科大讯飞
function Hmacstr2(e, key) {
  let result = CryptoJS.HmacSHA1(e, key);
  let sig = result.toString(CryptoJS.enc.Base64);
  // sig = encodeURI(sig);
  // sig = urlencode(sig);
  return sig;
}

export default { QMFun, QMdata, KDFun };
